package com.kayak.service.resource.transbase;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.RpcException;
import com.kayak.dbutil.dao.ComnDao;
import com.kayak.dbutil.redis.JedisEx;
import com.kayak.dubbo.bean.RpcBean;
import com.kayak.dubbo.global.RpcGlobal;
import com.kayak.dubbo.rpcInterface.RpcInterface;
import com.kayak.exception.ErrorException;
import com.kayak.kkutils.ErrorCode;
import com.kayak.kkutils.strutil.StringUtil;

public abstract class RpcBase implements RpcInterface {
	
	public Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected JedisEx jedisEx; 
	@Autowired
	protected ComnDao comnDao;
	@Override
	public RpcBean execute(RpcBean rpcBean) {
		try{
			rpcBean.setLocalMap(rpcBean.getRequest());
			
			MDC.put(RpcGlobal.TRANS_ID, (String)rpcBean.getLocalParam(RpcGlobal.TRANS_ID));
			MDC.put(RpcGlobal.TRANS_CODE, (String)rpcBean.getLocalParam(RpcGlobal.TRANS_CODE));
			MDC.put(RpcGlobal.REQUEST_NODE, (String)rpcBean.getLocalParam(RpcGlobal.REQUEST_NODE));
			MDC.put(RpcGlobal.CUST_ID, (String)rpcBean.getLocalParam(RpcGlobal.CUST_ID));
			MDC.put(RpcGlobal.SERVICE_NAME, this.getClass().getSimpleName());
			
			log.info("****serviceRequest: "+rpcBean.getRequest().toString());
			
			init(rpcBean);
			check(rpcBean);
			doBusiness(rpcBean);
			
			this.addReturnMsg(rpcBean,ErrorCode.retCodeSuc,ErrorCode.retMsgSuc);
		}catch(Exception e){
			e.printStackTrace();
			if(e instanceof ErrorException){
				ErrorException e1 = (ErrorException) e;
				String retCode = StringUtil.isBlank(e1.getErroCode()) ? ErrorCode.ERROR_9999_CODE : e1.getErroCode();
				this.addReturnMsg(rpcBean, retCode, e1.getMessage());
			}else if(e instanceof RpcException){
				this.log.error("调用服务失败"+e.getMessage());
				this.addReturnMsg(rpcBean, ErrorCode.ERROR_9999_CODE, ErrorCode.ERROR_9999_MSG);
			}else{
				this.addReturnMsg(rpcBean,ErrorCode.ERROR_9999_CODE,ErrorCode.ERROR_9999_MSG);
			}
		}finally{
			//将request和localmap两个结构清空，只返回response
			rpcBean.setRequest(null);
			rpcBean.setLocalMap(null);
			log.info("****serviceResponse: "+rpcBean.getResponse().toString());
		}
		return rpcBean;
	}
	
	public abstract void init(RpcBean rpcBean) throws ErrorException;
	
	public abstract void check(RpcBean rpcBean) throws ErrorException;
	
	public abstract void doBusiness(RpcBean rpcBean) throws ErrorException;

	public abstract void doBusiness(RpcBean rpcBean,InputStream in) throws ErrorException;
	
	public RpcBean execute(RpcBean rpcBean,InputStream in)  {
		try{
			rpcBean.setLocalMap(rpcBean.getRequest());
			
			MDC.put(RpcGlobal.TRANS_ID, (String)rpcBean.getLocalParam(RpcGlobal.TRANS_ID));
			MDC.put(RpcGlobal.TRANS_CODE, (String)rpcBean.getLocalParam(RpcGlobal.TRANS_CODE));
			log.info("****serviceRequest: "+rpcBean.getRequest().toString());
			
			init(rpcBean);
			check(rpcBean);
			doBusiness(rpcBean,in);
			
			this.addReturnMsg(rpcBean,ErrorCode.retCodeSuc,ErrorCode.retMsgSuc);
		}catch(Exception e){
			e.printStackTrace();
			if(e instanceof ErrorException){
				ErrorException e1 = (ErrorException) e;
				String retCode = StringUtil.isBlank(e1.getErroCode()) ? ErrorCode.ERROR_9999_CODE : e1.getErroCode();
				this.addReturnMsg(rpcBean, retCode, e1.getMessage());
			}else if(e instanceof RpcException){
				this.log.error("调用服务失败"+e.getMessage());
				this.addReturnMsg(rpcBean, ErrorCode.ERROR_9999_CODE, ErrorCode.ERROR_9999_MSG);
			}else{
				this.addReturnMsg(rpcBean,ErrorCode.ERROR_9999_CODE,ErrorCode.ERROR_9999_MSG);
			}
		}finally{
			//将request和localmap两个结构清空，只返回response
			rpcBean.setRequest(null);
			rpcBean.setLocalMap(null);
			log.info("****serviceResponse: "+rpcBean.getResponse().toString());
		}
		return rpcBean;
	};
	
	public void addReturnMsg(RpcBean rpcBean,String returnCode,String returnMsg){
		rpcBean.addResponse(ErrorCode.RET_CODE, returnCode);
		rpcBean.addResponse(ErrorCode.RET_MSG, returnMsg);
	}
	
}
